package ioperations;
import java.util.Scanner;
import book.BookList;
import book.Book;

public class AddOperation implements IOPeration{
    public void work(BookList booklist){
        System.out.println("新增图书...");

        //判满
        if(booklist.books.length==booklist.GetUsedSize()){
            System.out.println("书架已经满了，不能再加书了");
            return;
        }

        //构建新的对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        System.out.println("请输入作者：");
        String author = scanner.nextLine();

        System.out.println("请输入书的类型：");
        String type = scanner.nextLine();

        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        Book newBook = new Book(name,author,price,type);

        //3. 判断书架有没有这本书
        for (int i = 0; i < booklist.GetUsedSize(); i++) {
            Book book  =booklist.books[i];
            if(book.getName().equals(name)) {
                System.out.println("有这本书不能插入！");
                return;
            }
        }

        //4.插入这本书
        booklist.SetBook(booklist.GetUsedSize(),newBook);

        booklist.SetUsedSize(booklist.GetUsedSize()+1);

        System.out.println("新增图书成功！！！");

    }
}
